/**
 * @file LoadAllPage.as
 *  一页一页地连续加载数据页,直至所有数据加载完成
 loadallpage = new LoadAllPage(url,allDataloaded,0,0);
 function allDataloaded():void
 {
 for each (var str:String in loadallpage.dataArr)
 {
 arr.push(JSON.parse(str));
 }
 }

 * @author db0@qq.com
 * @version 1.0.1
 * @date 2015-05-06
 */
package
{
	import flash.events.Event;

	import flash.net.URLLoader;
	import flash.utils.setTimeout;

	public class LoadAllPage
	{
		private var curPageIndex:int = 0;
		private var startPageIndex:int = 0;
		private var endPageIndex:int = 0;
		private var url:String = "";
		private var completeFunc:Function= null;
		public var dataArr:Array = null;
		public function LoadAllPage(_url:String,_completeFunc:Function=null,_startPageIndex:int=0,_endPageIndex:int=0xfffff):void
		{
			url = _url;
			startPageIndex = _startPageIndex;
			endPageIndex = _endPageIndex;
			completeFunc = _completeFunc;
			loadPage(startPageIndex);
		}

		private function loadPage(i:int):void
		{
			curPageIndex = i;
			DataState.main.show("数据加载中... ");

			if(i > endPageIndex){
				completeFunc();
				return;
			}else if(int(FamilyData.numMem) <= PersonNode.list.length){
				completeFunc();
				return;
			}

			logs.adds(PersonNode.list.length + "/" + FamilyData.numMem,"curPageIndex:" + curPageIndex);
			SwfLoader.loadData(url+curPageIndex,loaded);
		}

		private function loaded(e:Event=null):void
		{
			if(e && e.type == Event.COMPLETE)
			{
				var loader:URLLoader = e.target as URLLoader;
				var data:String = String(loader.data);
				if(data && String(data).match(/^[\s\r\n]*\[*[\r\s\n]*\]*[\r\s\n]*$/m) == null){
					if(dataArr == null)
						dataArr = new Array();
					dataArr.push(data);
				}else{
					completeFunc();
					loader.close();
					loader = null;
					return;
				}
				loader.close();
				loader = null;
				if(curPageIndex >= endPageIndex)
					completeFunc();
				else
					loadPage(curPageIndex+1);
			}else{
				logs.adds("load Error, check the net states");
				completeFunc();
			}
		}

	}
}

